package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/utils"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"image/color"
)

func init() {
	widget.RegWidget("WidgetPolyLine", "多段线", NewNewLabel)
}

type PolyLine struct {
	*WidgetContainer
	Lines []fyne.CanvasObject
	*Action
	rootNode widgetcommon.IWidgetItem //父节点
	name     string
}

func NewPolyLine(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}

	_lines := &PolyLine{
		Lines:  []fyne.CanvasObject{},
		Action: &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{255, 0, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	_lines.WidgetContainer.ac = _lines.Action
	_lines.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _lines)
	_lines.WidgetContainer.SetObjects(append([]fyne.CanvasObject{}, _lines.Lines...))
	_lines.SetBase(_lines, rootNode)
	_lines.ExtendBaseWidget(_lines)
	_lines.name = name

	return _lines
}
func (l *PolyLine) SetProperty(key, value string, isProperty bool) {
	l.IUserInfo.SetProperty(key, value, isProperty)
	switch key {
	case "Color":
		color1, err := utils.ParseColor(value)
		if err == nil {
			for _, line := range l.Lines {
				if _, ok := line.(*canvas.Line); !ok {
					continue
				}
				line.(*canvas.Line).StrokeColor = color1
			}
		}
	}
}

func (l *PolyLine) MinSize() fyne.Size {
	if len(l.Lines) == 0 {
		return fyne.NewSize(0, 0)
	}
	return l.Lines[0].MinSize()
}
